"use strict";
const mongoose = require("mongoose");
const model = mongoose.model("LightApp");

const save = (conditions) => {
  return model.create(conditions);
}

const remove = (conditions) => {
  return model.remove(conditions);
}

const update = (conditions) => {
  return model.update(conditions);
}

const findOne = (conditions) => {
  return model.findOne(conditions);
}

const find = (conditions) => {
  return model.find(conditions);
}

const findOneAndUpdate = (conditions, body) => {
  return model.findOneAndUpdate(conditions, body, {new: true});
}

const getTotal = (conditions) => {
  const {orgId, domainId, keyword} = conditions;
  return model.count({
    orgId,
    domainId,
    $or: [
      {
        name: {
          $regex: new RegExp(keyword, 'i')
        }
      }
    ]
  });
}

const findAndPaging = (conditions) => {
  const {orgId, domainId, pageIndex, pageSize, keyword} = conditions;
  return model.find({
    orgId,
    domainId,
    $or: [
      { //多条件，数组
        name: {
          $regex: new RegExp(keyword, 'i')
        }
      }
    ]
  }).skip((pageIndex - 1) * pageSize).limit(pageSize)
}

module.exports = {
  save,
  remove,
  update,
  findOne,
  find,
  findOneAndUpdate,
  getTotal,
  findAndPaging
}
